<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="" prop="keyword">
                    <el-input class="w-[280px]" v-model="queryParams.keyword" placeholder="真实姓名/手机号"/>
                </el-form-item>
                <el-form-item label="" prop="status">
                    <el-select
                        v-model="queryParams.status"
                        class="w-[280px]"
                        clearable
                        placeholder="状态"
                    >
                        <el-option v-for="(item, index) in dictData.lawyer_status" :key="index" :label="item.name" :value="item.value"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="编号" prop="id" min-width="50" />
                <el-table-column label="所属地区" prop="areaName" min-width="80" />
                <el-table-column label="真实姓名" prop="realname" min-width="100" />
                <el-table-column label="联系方式" prop="mobile" min-width="100" />
                <el-table-column label="律所地址" prop="lawOfficeAddress" min-width="100" />
                <el-table-column label="审核状态" prop="status" min-width="100" >
                    <template #default="{ row }">
                        <dict-value :options="dictData.lawyer_status" :value="row.status" />
                    </template>
                </el-table-column>
                <el-table-column label="所属销售" prop="salesmanName" min-width="80" />
                <el-table-column label="推荐人" prop="cooperatorName" min-width="80" />
                <el-table-column label="操作" width="220" fixed="right">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="handleDetail(row)">
                            详情
                        </el-button>
                        <el-button v-if="row.status === 0" type="primary" link @click="changeStatus(row.id, 1)">
                            通过
                        </el-button>
                        <el-button v-if="row.status === 0" type="danger" link @click="changeStatus(row.id, 2)">
                            驳回
                        </el-button>
                        <el-button v-perms="['lawyer:edit']" type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            :dict-data="dictData"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="lawyer">
import { lawyerDelete, lawyerLists, lawyerEditState } from '@/api/lawyer'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {useDictData} from "@/hooks/useDictOptions";
import {adminStatus} from "@/api/perms/admin";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    keyword: '',
    status: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: lawyerLists,
    params: queryParams
})

const { dictData } = useDictData<{
    lawyer_status: any[],
    job_type: any[],
    sex: any[],
    sex2: any[],
}>(['lawyer_status','job_type', 'sex', 'sex2'])

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDetail = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('detail')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await lawyerDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

const changeStatus = async (id: number, status: number) => {
    try {
        await feedback.confirm(`确定${status===1 ? '通过' : '驳回'}？`)
        await lawyerEditState({ id, status })
        feedback.msgSuccess('修改成功')
        getLists()
    } catch (error) {
        getLists()
    }
}

getLists()
</script>
